
package com.jf.cloud.multishop.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.multishop.constant.ShopStatus;
import com.jf.cloud.api.multishop.vo.ShopDetailVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.dto.AuditingInfoDTO;
import com.jf.cloud.multishop.dto.ShopAuditingDTO;
import com.jf.cloud.multishop.service.ShopAuditingService;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.vo.ShopAuditingInfoVO;
import com.jf.cloud.multishop.vo.ShopAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 商家审核信息
 *
 * @author zz
 * @date 2021-02-01 17:10:35
 */
@RestController("platformShopAuditingController")
@RequestMapping("/p/shop_auditing")
@Tag(name = "platform-平台审核信息")
public class ShopAuditingController {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private ShopAuditingService shopAuditingService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @GetMapping("/page")
    @Operation(summary = "分页查询" , description = "分页查询")
    public ServerResponseEntity<PageVO<ShopAuditingInfoVO>> getShopAuditingPage(PageDTO pageDTO, AuditingInfoDTO auditingInfoDTO) {
        checkAuthorized();
        return ServerResponseEntity.success(shopAuditingService.auditingInfoPage(pageDTO, auditingInfoDTO));
    }

    @GetMapping("/{shopId}")
    @Operation(summary = "根据店铺id查询详情信息" , description = "根据店铺id查询详情信息")
    public ServerResponseEntity<ShopAuditingVO> getShopAuditing(@PathVariable Long shopId) {
        checkAuthorized();
        ShopAuditingVO shopAuditing = shopAuditingService.getByshopId(shopId);
        if (Objects.isNull(shopAuditing)){
            shopAuditing = new ShopAuditingVO();
        }
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (BooleanUtil.isFalse(permission)){
            shopDetailVO.setContactPhone(PhoneUtil.hideBetween(shopDetailVO.getContactPhone()).toString());
            if (PrincipalUtil.isMobile(shopDetailVO.getMerchantAccount())){
                shopDetailVO.setMerchantAccount(PhoneUtil.hideBetween(shopDetailVO.getMerchantAccount()).toString());
            }
        }
        if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE.value())) {
            shopAuditing.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OPEN_AWAIT_AUDIT.value())) {
            shopAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(shopId, SysTypeEnum.MULTISHOP.value());
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            shopDetailVO.setMerchantAccount(authAccountVO.getPhone());
            shopDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        shopAuditing.setShopDetail(shopDetailVO);
        return ServerResponseEntity.success(shopAuditing);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核信息(审核商家)" , description = "审核信息(审核商家)")
    public ServerResponseEntity<Void> audit(@Valid @RequestBody ShopAuditingDTO shopAuditingDTO) {
        checkAuthorized();
        shopDetailService.audit(shopAuditingDTO);
        shopDetailService.removeCacheByShopId(shopAuditingDTO.getShopId());

        return ServerResponseEntity.success();
    }

    /**
     * 校验权限
     */
    private void checkAuthorized () {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
    }
}
